package com.jeesite.modules.task.query.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.annotation.RepeatSubmit;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.RoleUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.dict.entity.DcSupTaskInfoDict;
import com.jeesite.modules.task.dict.service.DcSupTaskInfoDictService;
import com.jeesite.modules.task.query.entity.*;
import com.jeesite.modules.task.query.service.ComPreHenceQueryService;
import com.jeesite.modules.utils.OfficeUtil;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping(value = "${adminPath}/query/ComPreHenceQuery")
@RequiredArgsConstructor
public class ComPreHenceQueryController extends BaseController {

    final private ComPreHenceQueryService comPreHenceQueryService;

    final private UserService userService;

    @Autowired
    private DcSupTaskInfoService dcSupTaskInfoService;
    @Autowired
    private DcSupTaskInfoDictService dcSupTaskInfoDictService;

    @RequiresPermissions("query:ComPreHenceQuery:view")
    @RequestMapping(value = "index")
    public String index(DcSupTaskInfo dcSupTaskInfo, Model model) {
        // 查询条件，设置当前年
        dcSupTaskInfo.setTaskYear(DateUtils.getYear());
        List yearList = OfficeUtil.getYearList(null);
        model.addAttribute("yearList", yearList);

        DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
        dcSupTaskInfoDict.setDictStatus("0");
        dcSupTaskInfoDict.setOrderBy("dict_sort acs");
        List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
        model.addAttribute("dicts", dicts);
        // 查询牵头领导
        User user  = new User();
        user.setRoleCode("100016");
        user.setUserType("employee");
        List<User> list = userService.findList(user);
        model.addAttribute("userList",list);

        model.addAttribute("dcSupTaskInfodcSupTaskInfo",dcSupTaskInfo);
        return "modules/task/query/dcSupTaskComprehensiveQuery";
    }

    @RequiresPermissions("query:ComPreHenceQuery:view")
    @RequestMapping(value = "list")
    @ResponseBody
    public Page<ComPreHenceQueryEntity> list(ComPreHenceQueryEntity queryEntity, HttpServletRequest request, HttpServletResponse response){
        queryEntity.setPage(new Page<>(request,response));
//        if(RoleUtils.hasCurrentUserRole("province_task_watch")|| UserUtils.getUser().getUserCode().equals("system")){
//            queryEntity.setRemarks("0");
//        }else{
//            queryEntity.setRemarks("1");
//        }
        return comPreHenceQueryService.findComPreHencePage(queryEntity);
    }

    /**
     * 导出excel
     *
     * @param response
     * @throws Exception
     */
    @RequestMapping("exportData")
    public void exportExcel(ComPreHenceQueryEntity queryEntity, HttpServletResponse response) throws Exception {
        //默认Excel名称
        String fileName = "综合查询" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";


        List<ComPreHenceQueryExport> list = comPreHenceQueryService.findComPreHenceListAndExport(queryEntity);
        List<String> column = queryEntity.getExportColumn();
        // 设置表头各列的宽度
        List<Integer> headerWidthList = new ArrayList<>();
        for (String str : column) {
            if ("反馈内容".equals(str)) {
                headerWidthList.add(10000);
            } else if ("任务内容".equals(str)) {
                headerWidthList.add(20000);
            } else if ("整理内容".equals(str)) {
                headerWidthList.add(10000);
            } else {
                // 使用默认宽度
                headerWidthList.add(-1);
            }
        }
        ExcelExport ee = new ExcelExport("综合查询", column, headerWidthList);
        for (ComPreHenceQueryExport al : list) {
            // 创建空白行
            Row row = ee.addRow();
            // 添加列号
            int index = 0;
            for (String str : column) {
                if (str.equals("督办字号")) {
                    String rt = al.getMessageNumber();
                    ee.addCell(row, index++, rt);
                }
                if (str.equals("任务标题")) {
                    String rt = al.getTaskTitle();
                    ee.addCell(row, index++, rt);
                }
                if (str.equals("任务类型")) {
                    String rt = al.getTaskTypeName();
                    ee.addCell(row, index++, rt);
                }
                if (str.equals("任务内容")) {

                    String rt = al.getTaskContent();
                    ee.addCell(row, index++, rt);
                }
                if (str.equals("牵头领导")) {

                    String rt = al.getTaskLeaderName();
                    ee.addCell(row, index++, rt);
                }
                if (str.equals("牵头责任单位")) {

                    String rt = al.getTaskLeaderOrgName();
                    ee.addCell(row, index++, rt);
                }
                if (str.equals("任务状态")) {

                    String rt = al.getTaskStatus();
                    ee.addCell(row, index++, rt);
                }
                if (str.equals("反馈内容")) {

                    String rt = al.getFeedbackStr();
                    ee.addCell(row, index++, rt);
                }
                if (str.equals("整理内容")) {

                    String rt = al.getArrangeStr();
                    ee.addCell(row, index++, rt);
                }
            }
        }

        ee.write(response, fileName);
    }

    /**
     * 导出细化分解excel
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("exportRefineExcel")
    public void exportRefineExcel(ComPreHenceQueryEntity queryEntity, HttpServletRequest request, HttpServletResponse response) throws Exception {
        comPreHenceQueryService.exportRefineExcelNew(queryEntity,request,response);
//        comPreHenceQueryService.exportRefineExcel(queryEntity, request, response);
    }

    @RequestMapping(value = "changeSortNumForm")
    public String changeSortNumForm(ComPreHenceQueryEntity queryEntity, Model model){
        // 补充任务信息
        DcSupTaskInfo dcSupTaskInfo = dcSupTaskInfoService.get(queryEntity.getTaskId());
        queryEntity.setTaskTitle(dcSupTaskInfo.getTaskTitle());
        // 返回页面
        model.addAttribute("queryEntity", queryEntity);
        return "modules/task/query/dcSupTaskComprehensiveChange";
    }

    @RequestMapping(value = "changeSortNum")
    @ResponseBody
    @RepeatSubmit
    public String changeSortNum(String taskId, String sortNum){
        comPreHenceQueryService.changeSortNum(taskId, sortNum);
        return renderResult(Global.TRUE, text("修改任务排序编号成功！"));
    }

    @RequestMapping("btnExportPiShi")
    public void btnExportPiShi(ComPreHenceQueryEntity queryEntity, HttpServletRequest request, HttpServletResponse response) throws Exception {
        comPreHenceQueryService.btnExportPiShi(queryEntity, request, response);
    }
}
